package com.pinoc.demo.service.user.impl;

import com.pinoc.demo.bean.security.RoleEntry;
import com.pinoc.demo.bean.security.UserEntry;
import com.pinoc.demo.domain.user.User;
import com.pinoc.demo.mapper.RolesMapper;
import com.pinoc.demo.mapper.UserMapper;
import com.pinoc.demo.service.user.IUserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yinpeng
 */
@Service
public class UserServiceImpl implements IUserService {

	@Resource
	private UserMapper userMapper;
	@Resource
	private RolesMapper rolesMapper;

	@Override
	public List<User> selectAll() {
		return userMapper.selectAll();
	}

	@Override
	public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
		UserEntry user = userMapper.loadUserByUsername(s);
		if (user == null) {
			//避免返回null，这里返回一个不含有任何值的User对象，在后期的密码比对过程中一样会验证失败
			return new UserEntry();
		}
		//查询用户的角色信息，并返回存入user中
		List<RoleEntry> roles = rolesMapper.getRolesByUid(user.getId());
		user.setRoles(roles);
		return user;
	}
}
